<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Restock;
use App\Models\Goods;
use App\Models\GoodsPrice;
use App\Models\Manufacturer;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class RestockController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Restock(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('goods_name','产品名称')->display(function() {
                return Goods::find($this->goods_id)->goods_name;
            });
            $grid->column('manufacturer_name','厂家名称')->display(function() {
               $manufacturer_id = Goods::find($this->goods_id)->manufacturer_id;
               return Manufacturer::find($manufacturer_id)->manufacturer_name;
            });
            $grid->column('now_date');
            $grid->column('num');
            $grid->column('goods_price','进货价')->display(function(){
                $goods_price_info = GoodsPrice::where('goods_id',$this->goods_id)->where('now_date',date("Y-m-d",time()))->first();
                if($goods_price_info){
                    $goods_price = $goods_price_info->p_price;
                }else{
                    $goods_price_info = GoodsPrice::where('goods_id',$this->goods_id)->orderBy('now_date','desc')->first();
                    $goods_price = $goods_price_info->p_price??0;
                }
                $this->goods_price = $goods_price;
                return $goods_price;
            });
            $grid->column('price_date','进货价录入时间')->display(function(){
                //p($goods_id);
                $goods_price_info = GoodsPrice::where('goods_id',$this->goods_id)->where('now_date',date("Y-m-d",time()))->first();
                if($goods_price_info){
                    $now_date = $goods_price_info->now_date??'';
                }else{
                    $goods_price_info = GoodsPrice::where('goods_id',$this->goods_id)->orderBy('now_date','desc')->first();
                    $now_date = $goods_price_info->now_date??'';
                }
                return $now_date;
            });
            $grid->column('total_price','进货总金额')->display(function(){
                return $this->goods_price * $this->num;
            });

            $grid->filter(function (Grid\Filter $filter) {
                // 模糊搜索标题
                $filter->like('goods_name', '产品名称');

                // 下拉筛选分类
                $goodsList = Goods::pluck('goods_name','id')->toArray();
                //p($goodsList);
                $filter->equal('goods_id', '产品名称')->select($goodsList);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Restock(), function (Show $show) {
            $show->field('id');
            $show->field('goods_id');
            $show->field('now_date');
            $show->field('num');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Restock(), function (Form $form) {
            $form->display('id');
            $form->select('goods_id', '产品名称')->options(Goods::pluck('goods_name','id')->toArray());
            $form->date('now_date');
            $form->number('num');
        });
    }
}
